package com.securitydome09.config;

import com.securitydome09.pojo.SerializableRememberMeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class MyPersistentTokenRepository implements PersistentTokenRepository {

    private static final String TOKEN_PREFIX = "remember-me:";


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private static final long TOKEN_VALIDITY_SECONDS = 7 * 24 * 60 * 60; // 7 天

    @Override
    public void createNewToken(PersistentRememberMeToken token) {
        System.out.println(token.getDate());
        System.out.println(token.getTokenValue());
        System.out.println(token.getSeries());
        System.out.println(token.getUsername());
        String redisKey = TOKEN_PREFIX + token.getSeries();
        SerializableRememberMeToken serializableToken = new SerializableRememberMeToken(
                token.getUsername(), token.getSeries(), token.getTokenValue(), token.getDate()
        );
        redisTemplate.opsForValue().set(redisKey, serializableToken, TOKEN_VALIDITY_SECONDS, TimeUnit.SECONDS);
    }

    @Override
    public void updateToken(String series, String tokenValue, Date lastUsed) {
        String redisKey = TOKEN_PREFIX + series;
        PersistentRememberMeToken existingToken = (PersistentRememberMeToken) redisTemplate.opsForValue().get(redisKey);

        if (existingToken != null) {
            PersistentRememberMeToken updatedToken = new PersistentRememberMeToken(
                    existingToken.getUsername(),
                    series,
                    tokenValue,
                    lastUsed
            );
            redisTemplate.opsForValue().set(redisKey, updatedToken, TOKEN_VALIDITY_SECONDS, TimeUnit.SECONDS);
        }
    }

    @Override
    public PersistentRememberMeToken getTokenForSeries(String seriesId) {
        String redisKey = TOKEN_PREFIX + seriesId;
        SerializableRememberMeToken serializableToken = (SerializableRememberMeToken) redisTemplate.opsForValue().get(redisKey);
        if (serializableToken != null) {
            return new PersistentRememberMeToken(
                    serializableToken.getUsername(),
                    serializableToken.getSeries(),
                    serializableToken.getTokenValue(),
                    serializableToken.getDate()
            );
        }
        return null;
    }

    @Override
    public void removeUserTokens(String username) {
        // 删除该用户的所有令牌
        String pattern = TOKEN_PREFIX + "*";
        redisTemplate.keys(pattern).forEach(key -> {
            PersistentRememberMeToken token = (PersistentRememberMeToken) redisTemplate.opsForValue().get(key);
            if (token != null && username.equals(token.getUsername())) {
                redisTemplate.delete(key);
            }
        });
    }
}
